<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class S_Config extends CI_Config {

	//添加配置文件路径
	public function add_config_path($path)
	{
		$path = rtrim(str_replace('\\', '/', $path), '/').'/';
		array_unshift($this->_config_paths, $path);
	}

	//支持子目录绑定域名^_^，在config/domain.php中设置
	public function site_url($uri = '')
	{
		$uri = trim($uri, '/');
		
		if ($uri)
		{
			$uri = explode('/', $uri);
			$this->load('domain', TRUE, TRUE);
			$domain = $this->item('domain');
			if ($domain && ($key = array_search($uri[0], $domain)))
			{
				array_shift($uri);
				$base_url = rtrim($key, '/').str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
			}
		}
		
		if ( ! isset($base_url))
		{
			$base_url = $this->slash_item('base_url');
		}

		return $base_url.$this->item('index_page').'/'.implode('/', $uri);
	}
	
	/*
	 * 保存配置信息
	 * @access public
	 * @param $name 文件名
	 * @param $data 数据
	 * @return boolean
	 */
	public function save($name, $data = array())
	{
		$ci = &get_instance();
		$ci->load->helper('file');
		
		$data = var_export($data, TRUE);
		
		$data = "<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\n".$data;
		return write_file(FCPATH.'/config/'.$name.'.php', $data);
	}

}